<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<script src="../../js/mui.min.js"></script>
		<script type="text/javascript" src="../../js/jquery-1.10.1.js"></script>
		<script src="../../js/echarts.common.min.js"></script>
		<script type="text/javascript" src="../../js/GlobalSetting.js"></script>
		<!--<script type="text/javascript" src="../../js/Common/update.js" ></script>-->
		<link href="../../css/mui.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="../../css/iconfont.css" />
		<link rel="stylesheet" href="../../css/user.css" />
		<link rel="stylesheet" href="../../css/1px-hack.css" />
		<link rel="stylesheet" type="text/css" href="../../css/mui.picker.min.css" />
		<script src="../../js/Common/AjaxHelper.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../js/mui.picker.min.js" type="text/javascript" charset="utf-8"></script>
		<style type="text/css">
			#doneList img {
				display: block;
				width: 25px;
				height: 25px;
			}
		</style>
		<script type="text/javascript">
			GlobalVar.CurViewId = "HomePage";
			mui.init();
			var currentUser = "";
			var currentUserUrl = "";
			mui.plusReady(function() {
				currentUser = EPMUserInfo.LoadEPMUserInfo();
				mui(".mui-scroll-wrapper").scroll({
					deceleration: 0.0005
				});
				//激活Echarts
				BindEcharts();    
				//安卓点击退出确认
				mui.back = function() {
					var btnArray = ['取消', '确认'];
					mui.confirm('是否退出应用', '提示', btnArray, function(e) {
						if(e.index == 1) {
							plus.runtime.quit();
							//调用h5+的退出程序指令
						}
					})
				};
				//9.12添加，需要切换项目的代码，先写在这具体怎么执行再说
				$("#doneList").on("tap", function() {
					AjaxHelper.ajaxRequest(ServiceGlobal.ServiceUrl + "User/projectList", {
						id: currentUser.Id
					}, function(json) {
						
						var proContent = [];
						for(var i = 0; i < json.length; i++) {
							var obj = {};
							obj.text = json[i].PRJINFO_NAME;
							obj.value = json[i].PRJINFO_ID;
							proContent[i] = obj;
						};
						//给picker赋值
						//如果点击确定
						var picker = new mui.PopPicker();
						picker.setData(proContent);
						picker.show(function(selectItems) {
							var btnArray = ['否', '是'];
							mui.confirm(selectItems[0].text, '是否选择该项目？', btnArray, function(e) {
								if(e.index == 1) {
									//点击选中的时候，切换项目的id，也就是覆盖本地存储的项目id，并且刷新首页
									var user = plus.storage.getItem("User");
									var changeStorage = JSON.parse(user);
									console.log(selectItems[0].value)
									changeStorage.Themes = selectItems[0].value;
									plus.storage.setItem("User", JSON.stringify(changeStorage));
									console.log(plus.storage.getItem("User"));
									location.reload();
								}
							})
						});
					}, function(json) {
						mui.toast("切换项目请求失败")
					})
				})
				//获取权限菜单
				AjaxHelper.ajaxRequest(ServiceGlobal.ServiceUrl + "HomeData/GetCurrUserInfoModel/", {
					"Id": currentUser.Id
				}, function(json) {
					//					console.log(json)
					//                  请求加载九宫格的模块
					if(json) {
						//						console.log(json)
						var liStr = "";
						localStorage.setItem("IsOA", 0)  
						//给本存储一个值
						for(i = 0; i < json.length; i++) {
							liStr += "<li class=\"mui-col-sm-4 mui-col-xs-4 ui-border-r ui-border-b\" id=" + json[i].Code + " url=" + json[i].NavigateUrl + "><a href=\"javascript:;\"><img src=" + json[i].ImageUrl + " /><span>" + json[i].FullName + "</span></a></li>";
							//oa如果释放则就要把暂无数据给去掉
							//							console.log(json[i].Code);
							if(json[i].Code === "Quality") {
								//								console.log(i);  
								//								console.log(json[i].NavigateUrl);
								currentUserUrl = json[i].NavigateUrl
								//								console.log(currentUserUrl);
							}
							//如果有oa这个模块，则要改变本地存储的值
							if(json[i].Code == "OA") {
								localStorage.setItem("IsOA", 1);
							}
							//							console.log(liStr);
						}
						//console.log(currentUser.UserName+"---"+currentUser.Password);
						//						console.log(liStr);
						$("#modelContent").append(liStr);
						//给九宫格赋值
						//						id=OA url=/Areas/OA/OAList.html
						//                      系统给的id和跳转的路径

						$("#modelContent").find("li").on("tap", function() {
							var id = $(this).attr("id");
							var herf = $(this).attr("url");
							//							console.log(id)
							//							console.log(herf)
							if(herf != "null") {
								mui.openWindow({  
									url: herf,
									id: id
								});
							} else {
								return;
							}
						});
					}
					plus.nativeUI.closeWaiting();
				}, function() {
					plus.nativeUI.closeWaiting();
					mui.toast("菜单模块" + MessageInfo.RequestError);
					//菜单模块请求异常
				});
				//**************上面是请求加载九宫格的的模块********
				//**************请求完菜单模块九宫格，之后开始处理待办事项，以及为待办事项赋值和处理
				BindDBlist(currentUser);
				//***********执行有参函数，下面对该函数解析
				//
				//打开待办更多
				$("#todoList").on("tap", function() {
					mui.openWindow({
						url: "WorkflowTask.html",
						id: "WorkflowTask",
						show: {
							aniShow: 'slide-in-right'
						}
					});
				});
				//返回当前页面并刷新
				window.addEventListener('refresh', function(e) {
					BindDBlist(currentUser);
					BindEcharts();
				});
				window.addEventListener('pageflowrefresh', function(e) {
					//解决质量管理点击意见的时候自动返回
					BindDBlist(currentUser);
					BindEcharts();
				});
				window.addEventListener('back', function(e) {
					//解决资金计划备用金点击意见的时候自动返回
					//					console.log("主页你给我刷新")
					BindDBlist(currentUser);
					BindEcharts();  
				});  
			});

			//绑定待办列表
			function BindDBlist(currentUser) {
				//函数的封装,以便多次执行该方法FlowInfo_UnitNodeId=(null)
				AjaxHelper.ajaxRequest(ServiceGlobal.ServiceUrl + "WFCtrls/ClaimedTasksIndex/", {
						orgId: currentUser.CompanyId,
						dutyId: currentUser.WorkgroupId,
						userId: currentUser.Id,
						projectId: currentUser.Themes,
						topSize: 3
					},
					function(json) {
						var isoa = localStorage.getItem("IsOA");
						$("#homeDB").find("li:gt(0)").remove();
						if(json.data.length > 0) {
							var li = "";
							for(i = 0; i < json.data.length; i++) {
								//排队合同登记、资金计划、备用金项，只保留计划信息
								//马朋飞2017.03.01添加 
								//删除此处时需要恢复下面代码，和上面的topSize：3属性
								//								if(json.data[i].workflowType == 26 || json.data[i].workflowType == 1 || json.data[i].workflowType == 2 || json.data[i].workflowType == 3 || json.data[i].workflowType == 8 || json.data[i].workflowType == 13) {
								//									li += "<li class=\"mui-table-view-cell\" refresh WorktaskInsId='" + json.data[i].WorktaskInsId + "' workflowType='" + json.data[i].workflowType + "' workTaskInsId='" + json.data[i].WorktaskInsId + "' operatorInsId=" + json.data[i].operatorInsId + " workflowInsId=" + json.data[i].workflowInsId + " State=" + json.data[i].State + "><a><h4>" + json.data[i].FlowInsCaption + "</h4></a></li>"
								//								}
								//                              li += "<li class=\"mui-table-view-cell\" refresh WorktaskInsId='" + json.data[i].WorktaskInsId + "' workflowType='" + json.data[i].workflowType + "' workTaskInsId='" + json.data[i].WorktaskInsId + "' operatorInsId=" + json.data[i].operatorInsId + " workflowInsId=" + json.data[i].workflowInsId + " State=" + json.data[i].State + "><a><h4>" + json.data[i].FlowInsCaption + "</h4></a></li>"  

								//删除上面时需要恢复下面    
								//								if(json.data[i].State == 2){
								//								li += "<li class=\"mui-table-view-cell\" refresh WorktaskInsId='" + json.data[i].WorktaskInsId + "' workflowType='" + json.data[i].workflowType + "' operatorInsId='" + json.data[i].operatorInsId + "' workflowId='" + json.data[i].workflowId + "' workflowInsId='" + json.data[i].workflowInsId + "' State='" + json.data[i].State + "'><a><h4>" + json.data[i].FlowInsCaption + "</h4></a></li>"
								//								}  
								//1.12现在需要展现的工作流太多了
								var switchVal = json.data[i].workflowType;
								switch(switchVal) {  
									case 1:
									case 2:
									case 3:
//									case 5:合同管理
									case 6:
									case 8:
									case 9:
									case 10:
									case 11:
									case 12:
									case 13:
									case 14:
									case 15:
									case 18:
									case 19:
									case 20:
									case 21:
									case 22:
									case 23:
									case 24:
									case 25:
									case 26:
									case 27:
									case 28:
									case 29:
									case 31:
									case 32:
										li += "<li class=\"mui-table-view-cell\" refresh WorktaskInsId='" + json.data[i].WorktaskInsId + "' workflowType='" + json.data[i].workflowType + "' operatorInsId='" + json.data[i].operatorInsId + "' workflowId='" + json.data[i].workflowId + "' workflowInsId='" + json.data[i].workflowInsId + "' State='" + json.data[i].State + "'><a><h4>" + json.data[i].FlowInsCaption + "</h4></a></li>"
										break;
									default:
										break;
								}

								//                              if(json.data[i].workflowType != 4 || json.data[i].workflowType != 7 || json.data[i].workflowType != 16 || json.data[i].workflowType != 17 || json.data[i].workflowType != 25){ 
								//                              	li += "<li class=\"mui-table-view-cell\" refresh WorktaskInsId='" + json.data[i].WorktaskInsId + "' workflowType='" + json.data[i].workflowType + "' operatorInsId='" + json.data[i].operatorInsId + "' workflowId='" + json.data[i].workflowId + "' workflowInsId='" + json.data[i].workflowInsId + "' State='" + json.data[i].State + "'><a><h4>" + json.data[i].FlowInsCaption + "</h4></a></li>"
								//                              }
							}
							//							console.log(li);
							$("#homeDB").find(".index-todo-empty").remove();
							$("#homeDB").find("li:gt(0)").remove();
							$("#homeDB").append(li);
							if(isoa == 1) {
								if(json.data.length < 3) {
									BindOADBlist(3 - json.data.length);
								}
								GetCount();
								//获取oa的数量
								var oacount = localStorage.getItem("OAcount");

								if(oacount == 0) {
									$("#homeDB").find("li:gt(0)").remove();
									EmptyDataFill.emptyLi("#homeDB");
									$("#homeDB").off('tap');
								}
							}
						} else {
							if(isoa == 1) {
								BindOADBlist(3);
								GetCount();
								var oacount = localStorage.getItem("OAcount");
							}
							if(oacount == 0) {
								$("#homeDB").find("li:gt(0)").remove();
								EmptyDataFill.emptyLi("#homeDB");
								$("#homeDB").off('tap');
							}
							//                      	console.log("就是空数据");
							//                          $("#homeDB").find("li:gt(0)").remove();
							//                          //如果是空数据,把除了第一个li的值全部移除
							//                         var cd = $("#homeDB li:gt(0)").length;
							////                            console.log(cd);
							//                          if(cd == 0) {
							//                          	//这个就是暂无数据的方法,但是不知道为什么添加的
							//                              EmptyDataFill.emptyLi("#homeDB");
							//                              //添加暂无数据方法
							//                              $("#homeDB").off('tap');
							//                              //解绑事件
							//                          }
						}
						//
						bundHomeDB();
					},
					function() {
						plus.nativeUI.closeWaiting();
						$("#homeDB").find("li:gt(0)").remove();
						mui.toast("待办列表" + MessageInfo.RequestError);
					});
			}
			//**********************上面的逻辑*********
			//1.先请求其他模块的数据，如果其他模块的数据条数少于3条，那么oa就显示剩下的条数
			//2.在里面判断，如果oa得待办和请求回来的数据都为0，则显示空值

			//绑定OA待办列表
			function BindOADBlist(rows) {
				AjaxHelper.ajaxRequest(ServiceGlobal.ServiceOAUrl + "DocApprove/GetTodoList", {
						page: 1,
						rows: rows,
						username: currentUser.UserName
					},
					function(json) {
						//服务器返回响应，根据响应结果，分析是否登录成功；
						if(json) {
							var data = json.DataSource;
							if(data && data.length > 0) {
								var li = "";
								for(i = 0; i < data.length; i++) {
									li += "<li class=\"mui-table-view-cell\" refresh WorktaskInsId='" + data[i].WorktaskInsId + "' workflowType='10' operatorInsId='" + data[i].operatorInsId + "' workflowInsId='" + data[i].workflowInsId + "' State='Ctrl'>" + data[i].FlowInsCaption + " <small style='color:#999;'>" + data[i].FlowCaption + "-" + data[i].pOperatedDes + "</small></li></li>"
								}
								//								console.log(li);
								$("#homeDB").append(li);
							}
						}

						//首页待办列表点击事件
						bundHomeDB();
					},
					function(err) {
						mui.toast(MessageInfo.RequestError);
					}
				);
			}

			function bundHomeDB() {

				//首页待办列表点击事件
				$("#homeDB").find("li:gt(0)").on("tap", function() {
					var urlStr = "";
					var idStr = "";
					if($(this).attr("workflowType") == "0") return;
					console.log(parseInt($(this).attr("workflowType")))
					switch(parseInt($(this).attr("workflowType"))) {
//						case 5: //合同待办处理。
//							urlStr = "/Areas/Investme/Contract/ContractDetails.html";
//							idStr = "ContractDetails";
//							break;
						case 1:
						case 2:
						case 3:
						case 8: //计量
							urlStr = "/Areas/Investme/Segment/WorkflowIndex.html";
							idStr = "WorkflowIndex";
							break;
						case 6:
						case 9:
						case 10:
							urlStr = "/Areas/OA/OADetail.html";
							idStr = "OADetail";
							break;
						case 11: //资金
						case 18:
							urlStr = "/Areas/Investme/Funds/FundsIndex.html";
							idStr = "FundsIndex";
							break;
						case 12:
						case 13:
						case 14:
						case 15: //备用金
							urlStr = "/Areas/Investme/Imprest/newImprestIndex.html";
							idStr = "newImprestIndex";
							break;
						case 26:
							if(currentUserUrl === '/Areas/Quality/Declare/Declare.html') {
								console.log("进入质检申报待处理页面详情");
								urlStr = "/Areas/Quality/Declare/DeclareAgain.html";
								idStr = "DeclareDoneDetail";
							} else if(currentUserUrl === '/Areas/Quality/Sample/SampleIndex.html') {
								console.log("进入质检抽检页面待处理详情页");
								urlStr = "/Areas/Quality/Sample/SampleDoneList.html";
								idStr = "SampleDoneList";
							} else if(currentUserUrl === '/Areas/Quality/Manage/ManageIndex.html') {
								urlStr = "/Areas/Quality/Manage/ManageDoneList.html";
								idStr = "ManageDoneList";
							}
							break;
					}
					//                          console.log(urlStr + "；；；；；；"  + idStr);
					console.log($(this).attr("workflowInsId"));
					console.log();
					console.log();
					mui.openWindow({
						url: urlStr,
						id: idStr,
						extras: {
							"workflowId": $(this).attr("workflowId"),
							'workflowInsId': $(this).attr("workflowInsId"),
							'operatorInsId': $(this).attr("operatorInsId"),
							'workflowType': $(this).attr("workflowType"),
							'preview': 'HomePage',
							'viewMode': $(this).attr("state"),
							'workTaskInsId': $(this).attr("workTaskInsId"),
							"title": $(this).text(),
							"wfCompleted": 0
						},
						show: {
							duration: 200,
							aniShow: "none"
						}
					});
				});

			}
			//获取OA待办数量
			function GetCount() {
				AjaxHelper.ajaxRequest(ServiceGlobal.ServiceOAUrl + "Home/GetData", {
					username: currentUser.UserName
				}, function(json) {
					localStorage.setItem("OAcount", json.NewWorkCount);
				}, function(err) {
					mui.toast(MessageInfo.RequestError);  
				});
			}
			//激活下拉刷新
			//			function ListRefresh() {
			//				setTimeout(function() {
			//					var currentUser = EPMUserInfo.LoadEPMUserInfo();
			//					BindDBlist(currentUser);
			//					mui("#Refresh").pullRefresh().endPulldownToRefresh();
			//				}, 1500);
			//			}
			//柱状图数据绑定
			function BindEcharts() {
				AjaxHelper.ajaxRequest(ServiceGlobal.ServiceUrl + "HomeData/GetSegmentPayDT/", {
					"Id": currentUser.Id
				}, function(json) {  
					if(json != null && json != "") {
						var data = eval("(" + json + ")");
						SetHistogram(data);
					}
					plus.nativeUI.closeWaiting();
				}, function(err) {
					plus.nativeUI.closeWaiting();
					mui.toast("统计" + MessageInfo.RequestError);
				});
			}

			//柱状图列表绑定
			function SetHistogram(data) {
				var myChart = echarts.init(document.getElementById('mainPic'));
				option = {
					//					title: {
					//						show: true,
					//						text: '已完成计量情况',
					//						left: 'center',  
					//						textStyle: {
					//							fontSize: 14,
					//							bottom: 20,
					//							color: '#aac6f2'
					//						}
					//					},
					tooltip: {
						trigger: 'axis',
						showDelay: 0,
						position: [10, 10],
						formatter: function(params) {
							if(!!params[0].value) {
								return params[0].name + ' <br/>' +
									'签约合同价：' + params[0].value + '<br/> ' +
									'已完成投资额：' + params[1].value + '<br/>' +
									'单位：元';
							} else {
								return '';
							}
						},
						axisPointer: {
							show: true,  
							type: 'shadow',
							lineStyle: {
								type: 'dashed',
								width: 1
							}
						}
					},
					legend: {
						data: [{
								name: '签约合同价'
							},
							{
								name: '已完成投资额'
							}
						]
					},
					grid: {
						show: false,
						left: 20,
						right: 20,
						bottom: 4,
						top: 26,  
						height: 'auto',
						containLabel: true
					},
					xAxis: [{
						type: 'category',
						axisLine: {
							onZero: true,
							lineStyle: {
								color: '#558de5',
								width: 1
							}
						},
						axisTick: {
							sohw: true,
							inside: true
						},
						data: data.code
					}],
					yAxis: [{
						axisLine: {
							onZero: true,
							lineStyle: {
								color: '#558de5',
								width: 1
							}
						},
						axisTick: {
							show: true,
							inside: true
						},
						axisLabel: {
							show: false,
							inside: true
						},
						splitLine: {
							show: false
						},
						min: 'dataMin',
						type: 'value'
					}],
					backgroundColor: '#2a71de',
					series: [{
						name: '签约合同价',
						type: 'bar',
						barWidth: '50%',
						barMaxWidth: 32,
						barMinHeight: 20,
						barGap: '-100%',
						itemStyle: {
							normal: {
								barBorderRadius: 4,
								color: '#558de5'
							},
							emphasis: {
								color: '#aac6f2'
							}
						},
						stack: '签约合同价',
						data: changeArr(data.htVal)
					}, {
						name: '已完成投资额',
						type: 'bar',
						barWidth: '50%',
						barMaxWidth: 32,
						barMinHeight: 20,
						barGap: '-100%',
						itemStyle: {
							normal: {
								barBorderRadius: 4,
								color: '#ee7e32'
							},
							emphasis: {
								color: '#ffc58e'
							}
						},
						stack: '已完成投资额',
						data: changeArr(data.jlVal)  
					}]
				};
				myChart.setOption(option);

			}
			function changeArr (arr) {
				var newArr = []
				for (var i = 0;i<arr.length;i++) {
					if (arr[i] == 0) {
						newArr[i] = "";
					}else{
						newArr[i] = arr[i];
					}
				}
				return newArr
			}
		</script>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-icon mui-pull-right" id="doneList">
				<img src="../../images/exchange.png" />
			</a>
			<h1 class="mui-title">现场管理</h1>
		</header>
		<div class="mui-content mui-scroll-wrapper" id="Refresh">
			<div class="mui-scroll">
				<!--<select id="selectPro">
					<option value="">请先选择质检单元</option>
				</select>-->
				<!--<input type="hidden" name="" id="" value="11111111" />-->
				<div class="testBanner" id="mainPic" style="height: 140px;"></div>
				<div class="divider"></div>
				<div class="index-func-box">
					<!--在这添加九宫格-->
					<ul id="modelContent" class="mui-row index-func-container"></ul>
				</div>
				<div class="divider"></div>
				<ul id="homeDB" class="mui-table-view">
					<li class="mui-table-view-cell mui-table-view-title index-func-title" id="todoList">
						<a class="mui-navigate-right">
							<h4>待办事项</h4>
						</a>  
					</li>
					<!--除了这个li保留,剩下的全部移除-->
				</ul>
				<div class="divider"></div>
			</div>
		</div>
	</body>
	<script src="../../js/Common/NetStatusListener.js" type="text/javascript" charset="utf-8"></script>

</html>